﻿using System;
using System.Collections.Generic;

namespace MoreExtentions
{
    public static partial class Linq
    {
        public static TItem MinItem<TItem, TValue>(this IEnumerable<TItem> collection, Func<TItem, TValue> func)
            where TValue : IComparable<TValue>
            where TItem : class
        {
            TItem maxItem = default(TItem);
            TValue maxValue = default(TValue);

            if (collection != null)
                foreach (var item in collection)
                    if (item != null)
                    {
                        TValue temp = func(item);
                        if (maxItem == null || temp.CompareTo(maxValue) < 0)
                        {
                            maxValue = temp;
                            maxItem = item;
                        }
                    }

            return maxItem;
        }
    }
}